﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.BLL.Calendar;
using Jzy.Model.Calendar;
using System.Collections.Generic;
using Jzy.Model;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Frame.Lib.Core.Dept;
using Frame.Lib.Core.User;


public partial class Calendar_MonthList : PageList
{
    

    private CalendarBO cal = new CalendarBO();
    private DateTime dTmp;//绑定一月数据时每天的时间
    private DataView dv;//一月日程视图

    private CalendarLevel CalLevel
    {
        get
        {
            return (CalendarLevel)int.Parse(Request["Level"]);
        }
    }
    private DateTime CurrentTime
    {
        get
        {
            if (ViewState["CurrentTime"] == null)
            {
                ViewState["CurrentTime"] = DateTime.Now;
            }
            return (DateTime)ViewState["CurrentTime"];
        }
        set
        {
            ViewState["CurrentTime"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CalendarBO.DeptUserSelect drpDeptUser = new CalendarBO.DeptUserSelect(drpDepts, drpUsers);
            drpDeptUser.Bind(CalLevel);
            if (Request["DateTime"] != null)
            {
                CurrentTime = DateTime.Parse(Request["DateTime"]);
            }
            BindView();
        }

        AddAttr();
        JSUtil.SetDivHeight("list", 80); 
    }

    protected string Truncate(object strName)
    {
        string str = StrUtil.Truncate(strName.ToString(), 10);
        if (str.Length < strName.ToString().Length)
        {
            str += "…";
        }
        return str;
    }

    protected string EditCal(object id)
    {
        string paras = "?ID=" + StrUtil.EncryptUrl(id.ToString());
        return PopForm(false, paras);
    }

    #region 增加属性
    /// <summary>
    /// 增加属性
    /// </summary>
    private void AddAttr()
    {
        lnkAdd.Attributes.Add("onclick", PopForm(true, ""));
        lnkWeekCal.HRef = "WeekList.aspx?DateTime=" + System.Web.HttpUtility.UrlEncode(CurrentTime.ToString()) + "&Level=" + (int)CalLevel;
    }
    #endregion

    private void BindView()
    {
        //一月的日程   
        dv = cal.GetCurrentView(CalLevel,CurrentTime, drpDepts, drpUsers).DefaultView;

        //一个月的列表
        // DateTime dTmp = Convert.ToDateTime(strTime);
        dTmp = new DateTime(CurrentTime.Year, CurrentTime.Month, 1);//第一行第一列的日期
        dTmp = dTmp.AddDays(-(int)dTmp.DayOfWeek);//赋初值

        DataTable dt = cal.CreateMonth(CurrentTime);

        lv.DataSource = dt.DefaultView;
        lv.DataBind();

        lblMonth.Text = CurrentTime.Year + "&nbsp;年&nbsp;" + CurrentTime.Month + "月&nbsp;";

    }

    protected void btnPre_Click(object sender, ImageClickEventArgs e)
    {
        CurrentTime = CurrentTime.AddMonths(-1);        
        BindView();
    }
    protected void btnNext_Click(object sender, ImageClickEventArgs e)
    {
        CurrentTime = CurrentTime.AddMonths(1);
        BindView();
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindView();
    }

    protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            for (int i = 0; i < 7; i++)
            {
                ListView lvCalendar = (ListView)e.Item.FindControl("lv" + i);
                lvCalendar.ItemDataBound += new EventHandler<ListViewItemEventArgs>(lvCalendar_ItemDataBound);
                dv.RowFilter = " EndTime>'" + dTmp.ToString() + "' and StartTime<'" + dTmp.AddDays(1).ToString() + "'";
                lvCalendar.DataSource = dv;
                lvCalendar.DataBind();

                HtmlControl td = (HtmlControl)e.Item.FindControl("td" + i);
                if (dTmp.ToShortDateString() == DateTime.Now.ToShortDateString())
                {
                    td.Attributes.Add("class", "cal_currday");
                }

                if (dTmp.Month != CurrentTime.Month)
                {
                    td.Attributes.Add("class", "cal_over_month_day");
                }


                td.Attributes.Add("ondblclick", PopForm(true, "?StartTime=" + Server.UrlEncode(dTmp.AddHours(9).ToString("yyyy-MM-dd HH:mm:ss"))));
                dTmp = dTmp.AddDays(1);
            }
        }
    }

    void lvCalendar_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {            
            Label lblTime = (Label)e.Item.FindControl("lblTime");
            ListViewDataItem item = (ListViewDataItem)e.Item;
            DataRowView drv = (DataRowView)item.DataItem;
            DateTime dStartTime = (DateTime)drv["StartTime"];
            
            if (dStartTime > dTmp && dStartTime < dTmp.AddDays(1))
            {
                string strStartTime = dStartTime.ToShortTimeString();
                lblTime.Text = "§" + strStartTime + "";
            }
            else
            {
                lblTime.Text = "§9:00";
            }
        }
    }

    protected void drpDepts_SelectedIndexChanged(object sender, EventArgs e)
    {
        DeptBO dept = new DeptBO();
        IEnumerable<UserInfo> users = null;
        if (drpDepts.SelectedValue == "all")
        {
            users = dept.GetUserList((int)dept.GetRootID());
        }
        else
        {
            users = dept.GetUserList(int.Parse(drpDepts.SelectedValue));
        }
        CalendarBO.DeptUserSelect.BindUsers(drpUsers, users);
        BindView();
    }
    protected void drpUsers_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindView();
    }
   
    protected string PopForm(bool isAdd, string paras)
    {
        string strTitle = "查看日程";
        if (isAdd)
        {
            strTitle = "增加日程";
        }
        return JSUtil.ShowForm(null,strTitle, null, GlobalVar.WebCurrPath + "Edit.aspx" + paras, 400, 320);
    }
    
}
